package cn.bearspark.api.service.main.impl;

import cn.bearspark.api.service.main.DraftService;
import cn.bearspark.api.vo.main.draft.DraftSimpleVO;
import cn.bearspark.api.vo.main.draft.DraftVO;
import cn.bearspark.api.vo.main.draft.SaveDraftVO;
import cn.bearspark.article.dto.draft.DraftSimpleDTO;
import cn.bearspark.article.dto.draft.SaveDraftDTO;
import cn.bearspark.article.rpc.DraftRpc;
import cn.bearspark.common.constant.GlobalConstant;
import cn.bearspark.common.dto.Page;
import cn.bearspark.common.util.ConvertBeanUtil;
import cn.bearspark.framework.web.starter.context.BSRequestContext;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author f
 */
@Service
public class DraftServiceImpl implements DraftService {

    @DubboReference(check = GlobalConstant.DUBBO_CHECK)
    private DraftRpc draftRpc;

    @Override
    public Long save(SaveDraftVO saveDraftVO) {
        long userId = BSRequestContext.getUserId();

        SaveDraftDTO saveDraftDTO = ConvertBeanUtil.convert(saveDraftVO, SaveDraftDTO.class);
        saveDraftDTO.setUserId(userId);

        return draftRpc.save(saveDraftDTO);
    }

    @Override
    public DraftVO get(Long draftId) {
        long userId = BSRequestContext.getUserId();

        return ConvertBeanUtil.convert(draftRpc.get(draftId, userId), DraftVO.class);
    }

    @Override
    public void delete(Long draftId) {
        long userId = BSRequestContext.getUserId();

        draftRpc.delete(draftId, userId);
    }

    @Override
    public Page<DraftSimpleVO> page(Integer pageNum, Integer pageSize) {
        long userId = BSRequestContext.getUserId();

        Page<DraftSimpleDTO> draftSimpleDTOPage = draftRpc.page(pageNum, pageSize, userId);

        Page<DraftSimpleVO> draftSimpleVOPage = new Page<>();
        draftSimpleVOPage.setTotal(draftSimpleDTOPage.getTotal());
        draftSimpleVOPage.setRecords(ConvertBeanUtil.convertList(draftSimpleDTOPage.getRecords(), DraftSimpleVO.class));
        return draftSimpleVOPage;
    }
}
